###
list = (req, res, next)->
    chain = db.get(name)
    q = req.query.q
    _start = req.query._start
    _end = req.query._end
    _sort = req.query._sort
    _order = req.query._order
    _limit = req.query._limit
    _embed = req.query._embed
    _expand = req.query._expand
    delete req.query.q
    delete req.query._start
    delete req.query._end
    delete req.query._sort
    delete req.query._order
    delete req.query._limit
    delete req.query._embed
    delete req.query._expand


    # Automatically delete query parameters that can't be found in the databases
    Object.keys(req.query).forEach (query) ->
        arr = db.get(name).value()
        for i in arr
            return if _.has(arr[i], query) ||
                query == 'callback' ||
                query == '_' ||
                query.indexOf('_lte') != -1 ||
                query.indexOf('_gte') != -1 ||
                query.indexOf('_ne') != -1 ||
                query.indexOf('_like') != -1
        delete req.query[query]
    if q

        # Full-text search
        q = q.toLowerCase()

        chain = chain.filter -> (obj)
            for key in obj
                value = obj[key]
                return true if (db._.deepQuery(value, q))

    Object.keys(req.query).forEach (key) ->
        # Don't take into account JSONP query parameters
        #@ jQuery adds a '_' query parameter too
    if (key != 'callback' && key != '_')
        # Always use an array, in case req.query is an array
        arr = [].concat(req.query[key])

        chain = chain.filter (element)->
            return arr
            .map utils.toNative
            .map -> (value)
                isDifferent = key.indexOf('_ne') != -1
                isRange = key.indexOf('_lte') != -1 || key.indexOf('_gte') != -1
                isLike = key.indexOf('_like') != -1
                path = key.replace(/(_lte|_gte|_ne|_like)$/, '')
                elementValue = _.get(element, path)

                if (isRange)
                    isLowerThan = key.indexOf('_gte') != -1

                    if (isLowerThan) then return value <= elementValue else return value >= elementValue
                else if (isDifferent)
                    return value != elementValue
                else if (isLike)
                    return new RegExp(value, 'i').test(elementValue)
                else
                    return _.matchesProperty(key, value)(element)
            .reduce (a, b)->
                return a || b

    # Sort
    if _sort
        _order = _order || 'ASC'
        chain = chain.sortBy (element) ->
            return _.get(element, _sort)
        chain = chain.reverse() if (_order == 'DESC')


    # Slice result
    if _end || _limit
        res.setHeader('X-Total-Count', chain.size())
        res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')

    _start = parseInt(_start, 10) || 0

    if _end
        _end = parseInt(_end, 10)
        chain = chain.slice(_start, _end)
    else if _limit
        _limit = parseInt(_limit, 10)
        chain = chain.slice(_start, _start + _limit)

    # embed and expand
    chain = chain
    .cloneDeep()
    .forEach (element)->
        embed(element, _embed)
        expand(element, _expand)
    res.locals.data = chain.value()
    next()###
